
C_SOURCES =  \
hal/RISCV/gcc/init.c \
hal/RISCV/gcc/handlers.c \
hal/RISCV/stubs/sbrk.c \
hal/RISCV/drivers/n200_func.c \
hal/GD32VF103_standard_peripheral/system_gd32vf103.c \
hal/GD32VF103_standard_peripheral/Source/gd32vf103_gpio.c \
hal/GD32VF103_standard_peripheral/Source/gd32vf103_rcu.c \
hal/GD32VF103_standard_peripheral/Source/gd32vf103_timer.c \
hal/GD32VF103_standard_peripheral/Source/gd32vf103_eclic.c \
hal/GD32VF103_standard_peripheral/Source/gd32vf103_exti.c \
hal/GD32VF103_standard_peripheral/Source/gd32vf103_pmu.c \
hal/GD32VF103_standard_peripheral/Source/gd32vf103_usart.c \
hal/GD32VF103_standard_peripheral/Source/gd32vf103_fmc.c \
hal/GD32VF103_standard_peripheral/Source/gd32vf103_adc.c \
hal/GD32VF103_standard_peripheral/Source/gd32vf103_dma.c \
hal/GD32VF103_usbfs_driver/Source/drv_usb_core.c \
hal/GD32VF103_usbfs_driver/Source/drv_usb_dev.c \
hal/GD32VF103_usbfs_driver/Source/drv_usbd_int.c \
hal/GD32VF103_usbfs_driver/Source/usbd_core.c \
hal/GD32VF103_usbfs_driver/Source/usbd_enum.c \
hal/GD32VF103_usbfs_driver/Source/usbd_transc.c \
hal/gd32vf103_hw.c \
hal/gd32vf103_it.c \
hal/cdc_acm_core.c \
hal/hal_cdc.c \
hal/hal_led.c \
hal/hal_tick.c \
hal/hal_senser.c \
hal/main.c

C_SOURCES +=  \
pt/timer.c \
app/app_led.c \
app/app_cdc.c \
app/app_senser.c \
app/app.c

C_INCLUDES =  \
-Ihal \
-Ihal/GD32VF103_standard_peripheral \
-Ihal/GD32VF103_standard_peripheral/Include \
-Ihal/GD32VF103_usbfs_driver/Include \
-Ihal/RISCV/drivers \
-I.

C_DEFS = -DGD32VF103C_START -DUSE_STDPERIPH_DRIVER -DUSE_USB_FS


ASM_SOURCES =  \
hal/RISCV/gcc/entry.S \
hal/RISCV/gcc/start.S

AS_INCLUDES = 

AS_DEFS = 


PREFIX = riscv-none-embed-
MACHINE = -march=rv32imac -mabi=ilp32 -msmall-data-limit=8
CFLAGS = $(MACHINE)
ASFLAGS = $(MACHINE)
LDSCRIPT = hal/RISCV/gcc/GD32VF103xB.lds
LIBS = -nostartfiles
LIBDIR = 
LDFLAGS = $(MACHINE) -specs=nano.specs -T$(LDSCRIPT)


###############################################################################
# Verbosity control. Use  make V=1  to get verbose builds.
V=0
ifeq ($(V),1)
  TRACE_CC =
  TRACE_LD =
  TRACE_AR =
  TRACE_AS =
  TRACE_SIZE =
  TRACE_COPY =
  TRACE_DUMP =
  Q=
else
  TRACE_CC   = @echo "  CC   " $<
  TRACE_LD   = @echo "  LD   " $@
  TRACE_AR   = @echo "  AR   " $@
  TRACE_AS   = @echo "  AS   " $<
  TRACE_SIZE = @echo "  SIZE " $@
  TRACE_COPY = @echo "  COPY " $@
  TRACE_DUMP = @echo "  DUMP " $@
  Q=@
endif



###############################################################################
#
BUILD_DIR = build
OUTPUT_DIR = binary
EXE = microampere2

###############################################################################
#
CC   = $(PREFIX)gcc
AS   = $(PREFIX)gcc -x assembler-with-cpp
COPY = $(PREFIX)objcopy
AR   = $(PREFIX)ar
SIZE = $(PREFIX)size
DUMP = $(PREFIX)objdump

OPT = -Og

CFLAGS += -Wall -fdata-sections -ffunction-sections -fshort-wchar
CFLAGS += -MMD -MF"$(@:%.o=%.d)"
CFLAGS += -fstack-usage
CFLAGS += -g $(OPT) $(C_DEFS) $(C_INCLUDES)

ASFLAGS += -Wall -fdata-sections -ffunction-sections
ASFLAGS += -MMD -MF"$(@:%.o=%.d)" 
ASFLAGS += -g $(OPT) $(C_DEFS) $(C_INCLUDES) $(AS_DEFS) $(AS_INCLUDES)

LDFLAGS += -g $(OPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(EXE).map,--cref -Wl,--gc-sections


###############################################################################
#

all: \
$(OUTPUT_DIR)/$(EXE).elf \
$(OUTPUT_DIR)/$(EXE).hex \
$(OUTPUT_DIR)/$(EXE).bin \
$(BUILD_DIR)/$(EXE).disasm \
$(BUILD_DIR)/$(EXE).size

# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(C_SOURCES:.c=.o))
DEPS = $(addprefix $(BUILD_DIR)/,$(C_SOURCES:.c=.d))

# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(ASM_SOURCES:.S=.o))
DEPS += $(addprefix $(BUILD_DIR)/,$(ASM_SOURCES:.S=.d))

$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
	$(Q)mkdir -p $(dir $@)
	$(TRACE_CC)
	$(Q)$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(<:.c=.lst) $< -o $@

$(BUILD_DIR)/%.o: %.S Makefile | $(BUILD_DIR)
	$(Q)mkdir -p $(dir $@)
	$(TRACE_AS)
	$(Q)$(AS) -c $(ASFLAGS) $< -o $@

$(OUTPUT_DIR)/$(EXE).elf: $(OBJECTS) Makefile | $(OUTPUT_DIR)
	$(TRACE_LD)
	$(Q)$(CC) $(OBJECTS) $(LDFLAGS) -o $@

$(BUILD_DIR)/$(EXE).size: $(OUTPUT_DIR)/$(EXE).elf | $(BUILD_DIR)
	$(TRACE_SIZE)
	$(Q)$(SIZE) $< | tee $@

$(OUTPUT_DIR)/%.hex: $(OUTPUT_DIR)/%.elf | $(OUTPUT_DIR)
	$(TRACE_COPY)
	$(Q)$(COPY) -O ihex $< $@

$(OUTPUT_DIR)/%.bin: $(OUTPUT_DIR)/%.elf | $(OUTPUT_DIR)
	$(TRACE_COPY)
	$(Q)$(COPY) -O binary -S $< $@

$(BUILD_DIR)/%.disasm: $(OUTPUT_DIR)/%.elf | $(BUILD_DIR)
	$(TRACE_DUMP)
	$(Q)$(DUMP) -d $< > $@

$(BUILD_DIR):
	$(Q)mkdir $@

$(OUTPUT_DIR):
	$(Q)mkdir $@

#######################################
# clean up
#######################################
clean:
	-rm -fR $(BUILD_DIR)

#######################################
# dependencies
#######################################
-include $(DEPS)
